<template>
    <div class="approvalDetails">
        <div class="approvalDetails_header">
            <div class="approvalDetails_header_navBar">
                {{ lookData.auditStatus == 0 ? '审批中' : lookData.auditStatus == 2 ? '已通过' : lookData.auditStatus == 1 ?
                    '已驳回' : '' }}
                <img class="approvalDetails_header_navBar_img" src="@/assets/porduct/zuo_jiantou.png" alt=""
                    @click="routerBack">
            </div>
        </div>
        <ListItems class="approvalDetails_ListItems" :row="lookData" :status="status" @click="handleGetDetails()"
            :types="'deatils'" />
        <div class="approvalDetails_box">
            <StepBar stepBarTitle="审批流程" :data="getStepBarData()" :auditStatus="lookData.auditStatus" :status="status"
                :hmMechanismsList="lookData.hmMechanismsList" />
        </div>

        <div class="approvalDetails_footer" v-if="lookData.auditStatus == 2">
            <div class="footer_right" @click="approvalClick()">
                补样进度
            </div>
        </div>
        <div class="approvalDetails_footer" v-if="lookData.auditStatus == 0">
            <div class="footer_danger" @click="rejectClick()">
                驳回补样
            </div>
            <div class="footer_pass" @click="passClick()">
                同意补样
            </div>
        </div>
    </div>
    <!-- 驳回补样 -->
    <van-overlay :show="overlayShow" @click="overlayShow = false" z-index="6001">
        <div class="wrapper" @click.stop>
            <div class="wrapper_div">
                <van-image :src="colseImg" @click="overlayShow = false"></van-image>
                <div class="title">驳回建议</div>
                <van-form @submit="onSubmit">
                    <van-field maxlength="50" show-word-limit v-model="message" rows="6" label="" type="textarea"
                        placeholder="请输入驳回建议" :border="false"
                        :rules="[{ required: true, trigger: 'onBlur', message: '请输入驳回建议' }]" />
                    <van-button class="button" round block type="primary" :loading="loading" loading-text="执行中..."
                        native-type="submit" v-throttle:click>
                        提交确认
                    </van-button>
                </van-form>
            </div>
        </div>
    </van-overlay>
    <!-- 同意补样 -->
    <van-overlay :show="passShow" @click="passShow = false" z-index="6001">
        <div class="wrapper" @click.stop>
            <div class="wrapper_div wrapper_divs">
                <van-image :src="colseImg" @click="passShow = false"></van-image>
                <div class="title">补样邀约</div>
                <div class="flex">
                    <div class="label">供应商：</div>
                    <div class="value">{{ lookData.supplierName }}</div>
                </div>
                <div class="flex">
                    <div class="label">商品名称：</div>
                    <div class="value">{{ lookData.styleName }}</div>
                </div>
                <div class="flex">
                    <div class="label">补样数量：</div>
                    <div class="value">
                        <van-stepper v-model="sampleNum" :min="1" :max="999" integer />
                    </div>
                </div>
                <div class="flex">
                    <div class="cancel" @click="passShow = false">取消</div>
                    <div class="save" @click="save">
                        <template v-if="loading">
                            <van-loading type="spinner" size="24px" color="#fff"> 执行中...</van-loading>
                        </template>
                        <template v-else>
                            确认
                        </template>
                    </div>
                </div>
            </div>
        </div>
    </van-overlay>
</template>

<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import ListItems from './wareConponents/listItemsNewui.vue' // 列表单个项
import StepBar from './wareConponents/stepBar/index.vue'  // 步骤条
import { getStockSampleById, refuse, pass } from '@/api/operation/index'
import { showFailToast, showSuccessToast } from 'vant'
const colseImg = new URL("@/assets/approval/close.png", import.meta.url).href;
const route: any = useRoute()
const router = useRouter()
const showLoading = ref(false)
const status = ref()
const lookData = ref<any>({})
const overlayShow = ref(false)
const message = ref('')
const loading = ref(false)

const passShow = ref(false)
// 补样数量
const sampleNum = ref()

/** 补样进度操作 */
const approvalClick = async () => {
    router.push('/sampleData')
}
/** 驳回补样 */
const rejectClick = async () => {
    overlayShow.value = true
}
/** 驳回建议提交 */
const onSubmit = async () => {
    loading.value = true
    try {
        await refuse({ id: lookData.value.id, auditRefuseRemark: message.value, productId: lookData.value.productId })
        showSuccessToast({ type: 'success', message: '驳回成功' });
        loading.value = false
        overlayShow.value = false
        router.back()
    } catch (error) {
        if (error == 'timeout') {
            showFailToast('驳回失败');
        }
        loading.value = false
    }
}
/** 同意补样 */
const passClick = async () => {
    passShow.value = true
    // 设置默认展示补样数量为最近一次邀约数量
    const data = lookData.value.hmSampleAuditLogVos.filter(item => item.auditStatus == 0);
    sampleNum.value = Number(data[0]?.requestSampleNum) || 1;
}
/** 同意补样提交 */
const save = async () => {
    loading.value = true
    try {
        await pass({ id: lookData.value.id, passNum: sampleNum.value, productId: lookData.value.productId, coreDeptId: lookData.value.requestDeptId })
        loading.value = false
        overlayShow.value = false
        router.push('/warehouseApprovalSuccess')
    } catch (error) {
        if (error == 'timeout') {
            showFailToast('补样失败');
        }
        loading.value = false
    }
}

// 需要展示步骤条的数据
const getStepBarData = () => {
    return lookData.value.hmSampleAuditLogVos
}


// 返回
const routerBack = () => {
    router.go(-1)
}

/** 获取详情 */
const handleGetDetails = () => {
    router.push({
        path: `/productHomeDetail/${route?.query?.productId}`,
    })
};

const getLook = async (id: any) => {
    showLoading.value = true
    const { data = {} } = await getStockSampleById({ id })
    lookData.value = data
    showLoading.value = false
}
onMounted(() => {
    status.value = Number(route?.query?.status)
    getLook(route?.query?.id)
})
</script>

<style lang="less" scoped>
.approvalDetails {
    width: 100%;
    height: 100vh;
    display: flex;
    flex-flow: column;
    background: #f6f8fa;

    .approvalDetails_header {
        padding-top: 45px;
        background: linear-gradient(#CDECFF 0%, #FFFFFF 100%);
        margin-bottom: 10px;

        .approvalDetails_header_navBar {
            padding: 15px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 500;
            font-size: 16px;
            color: #1A1B1C;
            position: relative;

            .approvalDetails_header_navBar_img {
                position: absolute;
                top: 50%;
                left: 15px;
                transform: translate(0%, -50%);
                width: 15px;
                height: 16px;
            }
        }
    }

    .approvalDetails_ListItems {
        margin-bottom: 15px;
    }

    .approvalDetails_box {
        flex: 1;
        min-height: 0;
        overflow-y: auto;
        padding: 0 15px 15px;
        box-sizing: border-box;
    }

    .approvalDetails_footer {
        width: 375px;
        height: 84px;
        background: #FFFFFF;
        box-shadow: 0px 0px 8px 0px rgba(189, 195, 204, 0.5);
        padding: 20px 15px;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;


        .bt_f_left {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-left: 15px;
            margin-right: 30px;

            .buma {
                width: 20px;
                height: 20px;
                background: url("@/assets/newui/details/icon_buma@2x.png");
                background-repeat: no-repeat;
                background-size: 100% 100%;
            }

            .bt_text {
                font-weight: 400;
                font-size: 12px;
                color: #0094FF;
                line-height: 12px;
                margin-top: 6px;
            }
        }

        .footer_danger {
            width: 162px;
            height: 44px;
            border-radius: 5px;
            border: 1px solid #FC2540;
            font-weight: 500;
            font-size: 16px;
            color: #FC2540;
            text-align: center;
            line-height: 44px;
        }

        .footer_pass {
            width: 163px;
            height: 44px;
            background: linear-gradient(146deg, #0064FF 0%, #008FFF 100%);
            border-radius: 5px;
            font-weight: 500;
            font-size: 16px;
            color: #FFFFFF;
            text-align: center;
            line-height: 44px;
        }

        .footer_center {
            width: 0px;
            flex: 2;
            height: 100%;
            border-radius: 5px;
            border: 1px solid #5FBBFF;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 400;
            font-size: 16px;
            color: #0094FF;
            margin-right: 10px;
        }

        .footer_right {
            width: 0px;
            flex: 3;
            height: 100%;
            background: linear-gradient(146deg, #0064FF 0%, #008FFF 100%);
            border-radius: 5px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: 500;
            font-size: 16px;
            color: #FFFFFF;
        }
    }
}

:deep(.van-stepper__input) {
    width: 60px !important;
    background-color: #fff;
    font-size: 16px;
    color: #0094FF;
    border-top: 1px solid #F3F3FF;
    border-bottom: 1px solid #F3F3FF;
    margin: 0 !important;
}

.wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;

    .wrapper_div {
        width: 315px;
        min-height: 327px;
        background-color: #fff;
        border-radius: 5px;
        position: relative;

        :deep .van-field__control {
            background: #F6FAFF !important;
        }

        ::placeholder {
            font-size: 14px;
            color: #B8BECA;
        }

        .title {
            font-weight: 500;
            font-size: 16px;
            color: #1A1B1C;
            width: 100%;
            text-align: center;
            margin: 30px 0 20px 0;
        }

        .van-image {
            width: 10px;
            height: 10px;
            position: absolute;
            right: 18px;
            top: 18px;
        }

        :deep .van-form {
            min-height: 265px;
        }

        .button {
            color: #FFFFFF;
            font-size: 14px;
            width: 285px;
            height: 44px;
            background: #0094FF;
            border-radius: 23px;
            margin: auto;
            display: flex;
            justify-content: center;
        }

        .flex {
            display: flex;
            align-items: flex-start;
            box-sizing: border-box;
            padding: 0 20px;
            margin-bottom: 35px;
            justify-content: space-between;

            .label {
                text-align: right;
            }

            .value {
                flex: 1;
            }

            .cancel {
                width: 135px;
                height: 44px;
                border-radius: 23px;
                border: 1px solid #0094FF;
                font-weight: 400;
                font-size: 16px;
                color: #0094FF;
                text-align: center;
                line-height: 44px;
            }

            .save {
                width: 135px;
                height: 44px;
                background: #0094FF;
                border-radius: 23px;
                font-weight: 600;
                font-size: 16px;
                color: #FFFFFF;
                text-align: center;
                line-height: 44px;
            }
        }
    }
}
</style>